<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="Title" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        导出
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column width="120px" align="center" label="用户Id">
        <template slot-scope="scope">
          <span>{{ scope.row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="提交时间">
        <template slot-scope="scope">
          <span>{{ scope.row.submitDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button size="medium " @click="handleCheckUser(row,$index)">
            查看
          </el-button>
          <el-button size="medium " @click="handleAccept(row,$index)">
            批准
          </el-button>
          <el-button type="danger" size="medium " @click="handleDeny(row,$index)">
            拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />

    <el-dialog title="申请用户信息" :visible.sync="userInfoVisible">
      <el-form :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="Name" prop="name">
          <el-input v-model="temp.name" disabled />
        </el-form-item>
        <el-form-item label="email" prop="email">
          <el-input v-model="temp.email" disabled />
        </el-form-item>
        <el-form-item label="phone" prop="phone">
          <el-input v-model="temp.phone" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="userInfoVisible = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchApply, accpetApply, deleteApply } from '@/api/apply'
import { findUser } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'ApplyList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      downloadLoading: false,
      listQuery: {
        page: 1,
        size: 20
      },
      userInfoVisible: false,
      temp: {
        name: 'not loaded',
        email: 'not loaded',
        phone: 'not loaded'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      const { page, size } = this.listQuery
      fetchApply({ page: page - 1, size }).then(response => {
        this.list = response.items
        this.total = response.total
        this.listLoading = false
      }, _ => {
        this.$notify({
          title: '失败',
          message: '加载失败,请尝试稍后在进入该界面',
          type: 'error',
          duration: 2000
        })
      })
    },
    handleCheckUser(row) {
      findUser(row.userId).then(res => {
        this.userInfoVisible = true
        this.temp = res
      })
    },
    handleAccept(row, index) {
      const userId = row.userId
      accpetApply({ userId }).then(res => {
        this.$notify({
          title: '成功',
          message: '批准成功',
          type: 'success',
          duration: 2000
        })
        this.list.splice(index, 1)
      }, _ => {
        this.$notify({
          title: '失败',
          message: '提交失败',
          type: 'error',
          duration: 2000
        })
      })
    },
    handleDeny(row, index) {
      this.$confirm('确定要拒绝吗', '提示')
        .then(() => {
          deleteApply(row).then(res => {
            this.$notify({
              title: '成功',
              message: '成功删除',
              type: 'success',
              duration: 2000
            })
            this.list.splice(index, 1)
          }, _ => {
            this.$notify({
              title: '失败',
              message: '删除失败',
              type: 'error',
              duration: 2000
            })
          })
        })
    },
    // resetTemp() {
    //   this.temp = {
    //     name: 'not loaded',
    //     email: 'not loaded',
    //     phone: 'not loaded'
    //   }
    //   return true
    // },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['id', 'name', 'password', 'email', 'phone']
        const data = this.formatJson(tHeader)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'user-list'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
